package cn.cf.kxx.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import cn.cf.kxx.R;
import cn.cf.kxx.model.Group;

public class WeatherAdapter extends BaseAdapter {

    private Context mContext;
    private JSONArray mDataList;

    public WeatherAdapter(Context context, JSONArray dataList) {
        mContext = context;
        mDataList = dataList;
    }
    @Override
    public int getCount() {
        return mDataList.length();
    }

    public void setmDataList(JSONArray mDataList) {
        this.mDataList = mDataList;
    }

    @Override
    public Object getItem(int position) {
        try {
            return mDataList.get(position);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        WeatherAdapter.ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.weather_item, parent, false);
            holder = new WeatherAdapter.ViewHolder();
            holder.temper = convertView.findViewById(R.id.temper);
            holder.which = convertView.findViewById(R.id.which);
            holder.header = convertView.findViewById(R.id.header);
            convertView.setTag(holder);
        } else {
            holder = (WeatherAdapter.ViewHolder) convertView.getTag();
        }

        JSONObject item = null;
        try {
            item = mDataList.getJSONObject(position);
            holder.temper.setText(item.getString("temperatureMin") +"-" +item.getString("temperatureMax") + "°C");
            switch (item.getString("skyCon")){
                case "晴（白天）":
                case "晴（夜间）":
                    holder.header.setImageResource(R.mipmap.qing);
                    break;
                case "多云（白天）":
                case "多云（夜间）":
                    holder.header.setImageResource(R.mipmap.duoyun);
                    break;
                case "阴":
                    holder.header.setImageResource(R.mipmap.yin);
                    break;
                case "轻度雾霾":
                case "中度雾霾":
                case "重度雾霾":
                    holder.header.setImageResource(R.mipmap.wuli);
                    break;
                case "小雨":
                    holder.header.setImageResource(R.mipmap.xiaoyu);
                    break;
                case "中雨":
                    holder.header.setImageResource(R.mipmap.zhongyu);
                    break;
                case "大雨":
                    holder.header.setImageResource(R.mipmap.dayu);
                    break;
                case "暴雨":
                    holder.header.setImageResource(R.mipmap.baoyu);
                    break;
                case "雾":
                    holder.header.setImageResource(R.mipmap.wuli);
                    break;
                case "小雪":
                    holder.header.setImageResource(R.mipmap.xiaoxue);
                    break;
                case "中雪":
                    holder.header.setImageResource(R.mipmap.zhongxue);
                    break;
                case "大雪":
                    holder.header.setImageResource(R.mipmap.daxue);
                    break;
                case "暴雪":
                    holder.header.setImageResource(R.mipmap.baoxue);
                    break;
                case "浮尘":
                    holder.header.setImageResource(R.mipmap.fuchen);
                    break;
                case "沙尘":
                    holder.header.setImageResource(R.mipmap.shachen);
                    break;
                case "大风":
                    holder.header.setImageResource(R.mipmap.dafeng);
                    break;
            }
            holder.which.setText(item.getString("content"));
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }


        return convertView;
    }

    class ViewHolder{
        TextView temper;
        TextView which;
        ImageView header;
    }
}
